package com.example.champtionthree;


import android.text.TextUtils;


import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;


import com.example.champtionthree.bean.ExamInfo;
import com.example.champtionthree.model.TestModel;
import com.example.champtionthree.view.ExamFragment;

import com.example.common.ICommonModel;

import com.example.common.base.BaseMvpActivity;
import com.example.common.constans.ApiConfig;
import com.example.common.constans.LoadType;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;



public class MainActivity extends BaseMvpActivity{

    private TabLayout tab;
    private ViewPager vp;
    private List<Fragment> mFragmentList=new ArrayList<>();
    private List<String> mTitleList=new ArrayList<>();
    private MyFragmentAdapter mMFragmentAdapter;



    @Override
    public int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void setUpView() {
        tab = (TabLayout) findViewById(R.id.tab);
        vp = (ViewPager) findViewById(R.id.vp);
        mMFragmentAdapter=new MyFragmentAdapter(getSupportFragmentManager(),mFragmentList,mTitleList);
        vp.setAdapter(mMFragmentAdapter);
        tab.setupWithViewPager(vp);
    }

    @Override
    public void setUpData() {
        presenter.getData(ApiConfig.TEST, LoadType.NORMAL);
    }

    @Override
    public ICommonModel setModel() {
        return new TestModel();
    }


    @Override
    public void onSuccess(int whichApi, int loadType, Object[] d) {

        switch (whichApi){
            case ApiConfig.TEST:
                ExamInfo info= (ExamInfo) d[0];
                List<ExamInfo.ItemInfo> data = info.data;
                for (int i = 0; i < data.size(); i++) {
                    mTitleList.add(data.get(i).name);
                    ArrayList<ExamInfo.SmallItemInfo> albums = data.get(i).albums;
                    mFragmentList.add(ExamFragment.newInstance(albums));

                }
                if (mFragmentList.size() <= 2) return;
                mMFragmentAdapter.notifyDataSetChanged();
                vp.setCurrentItem(2);
                break;
        }

        }



    @Override
    public void onFailed(Throwable pThrowable) {
        showLog(pThrowable!=null&&!TextUtils.isEmpty(pThrowable.getMessage())?pThrowable.getMessage():"傻逼赵成龙");
    }
}